import Vue from 'vue'
import Router from 'vue-router'
import beforeEachHooks from './beforeEachHooks'
import routes from './routes'

Vue.use(Router)

const createRouter = () =>
  new Router({
    // mode: 'history',
    routes: routes,
    linkActiveClass: 'active',
    linkExactActiveClass: 'exact-active',
    scrollBehavior: (to, from, savedPosition) => savedPosition || { x: 0, y: 0 },
  })

const router = createRouter()
Object.keys(beforeEachHooks).forEach(hook => {
  router.beforeEach(beforeEachHooks[hook])
})

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}
router.resetRouter = resetRouter

export default router
